home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.04 Apr 90 / List Demo Project / String Utilities < prev   
Encoding:
Text File  |  1989-08-03  |  3.8 KB  |  159 lines  |  [TEXT/PJMM]

  1. unit StringUtilities;
  2. interface
  3.     uses
  4.         Sane;
  5.     const
  6.         CR = char(13);
  7.         tab = char(9);
  8.         comma = char(44);
  9.  
  10.     function PtrToString (var theStr: Str255): ptr;
  11.     procedure HandleToStr255 (aHandle: handle;
  12.                                     var theString: str255;
  13.                                     var error: boolean);
  14.     procedure AppendString (theText: handle;
  15.                                     theString: str255;
  16.                                     var error: boolean);
  17.     function IsNumber (theText: Str255;
  18.                                     IsInteger: boolean): boolean;
  19.     function StringToReal (number: Str255): extended;
  20.     function RealToFixed (number: extended;
  21.                                     decimals: integer): Str255;
  22.     function RealToFormat (number: extended;
  23.                                     theForm: DecForm): Str255;
  24.     function RealToString (number: extended;
  25.                                     sigFigs: integer): Str255;
  26. implementation
  27. {*******************************************************}
  28.     function PtrToString (var theStr: Str255): ptr;
  29.     begin
  30.         PtrToString := ptr(longint(@theStr) + 1);
  31.     end;
  32. {*********************************}
  33.     procedure HandleToStr255;
  34.         var
  35.             i, len: integer;
  36.     begin
  37.         if not error then
  38.             begin
  39.                 theString := '';
  40.                 len := GetHandleSize(aHandle);
  41.                 if len > 255 then
  42.                     len := 255;
  43.                 for i := 1 to len do
  44.                     theString := concat(theString, charsHandle(aHandle)^^[i - 1]);
  45.             end;
  46.     end;
  47. {*********************************}
  48.     procedure AppendString;
  49.         var
  50.             err: OSErr;
  51.     begin
  52.         if not error then
  53.             begin
  54.                 err := PtrAndHand(PtrToString(theString), theText, length(theString));
  55.                 error := (err <> noErr);
  56.             end;
  57.     end;
  58. {*********************************}
  59.     function OrderOfMagnitude (number: extended): integer;
  60. {this is the order of magnitude which puts a number in scientific notation}
  61.         var
  62.             magnitude: extended;
  63.     begin
  64.         if number <> 0 then
  65.             begin
  66.                 magnitude := ln(abs(number)) / ln(10);
  67.                 if magnitude >= 0 then
  68.                     magnitude := trunc(magnitude)
  69.                 else if (trunc(magnitude) <> magnitude) then
  70.                     magnitude := trunc(magnitude) - 1;
  71.             end
  72.         else
  73.             magnitude := 1;
  74.         OrderOfMagnitude := trunc(magnitude);
  75.     end;
  76. {**************************************************}
  77.     function IsNumber (theText: Str255;
  78.                                     IsInteger: boolean): boolean;
  79.         var
  80.             oneExp, oneDec, badText: boolean;
  81.             i: integer;
  82.     begin
  83.         oneExp := false;
  84.         oneDec := false;
  85.         BadText := false;
  86.         for i := 1 to length(theText) do
  87.             case theText[i] of
  88.                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',': 
  89.                     ;{do nothing}
  90.                 'e', 'E': 
  91.                     if oneExp then
  92.                         badText := true
  93.                     else
  94.                         oneExp := true;
  95.                 '.': 
  96.                     if oneDec then
  97.                         badText := true
  98.                     else
  99.                         oneDec := true;
  100.                 '-', '–': 
  101.                     if not oneExp then
  102.                         BadText := (i > 1);
  103.                 otherwise
  104.                     BadText := true;
  105.             end;{case}
  106.  
  107.         if (oneExp or oneDec) and IsInteger then
  108.             badText := true;
  109.  
  110.         IsNumber := not BadText;
  111.     end;
  112. {*************************************************************************}
  113.     function StringToReal;
  114.     begin
  115.         StringToReal := Str2Num(number);
  116.     end;
  117. {*****************************************}
  118.     function RealToFixed;
  119.         var
  120.             order: integer;
  121.             tempStr: DecStr;
  122.             theForm: DecForm;
  123.     begin
  124.         order := OrderOfMagnitude(number);
  125.         theForm.digits := decimals;
  126.         theForm.style := FixedDecimal;
  127.         Num2Str(theForm, number, tempStr);
  128.         RealToFixed := tempStr;
  129.     end;
  130. {*****************************************}
  131.     function RealToFormat;
  132.         var
  133.             tempStr: DecStr;
  134.     begin
  135.         Num2Str(theForm, number, tempStr);
  136.         RealToFormat := tempStr;
  137.     end;
  138. {*****************************************}
  139.     function RealToString;
  140.         var
  141.             order: integer;
  142.             tempStr: DecStr;
  143.             theForm: DecForm;
  144.     begin
  145.         order := OrderOfMagnitude(number);
  146.         if (order > sigFigs) or (order < -1) then
  147.             begin
  148.                 theForm.style := FloatDecimal;
  149.                 theForm.digits := sigFigs - 1;
  150.             end
  151.         else
  152.             begin
  153.                 theForm.style := FixedDecimal;
  154.                 theForm.Digits := sigFigs - order - 1;
  155.             end;
  156.         Num2Str(theForm, number, tempStr);
  157.         RealToString := tempStr;
  158.     end;
  159. end.